| Conditions | 1 |
| Paths | 1 |
| Total Lines | 173 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import os from 'os'; |
||
| 20 | describe('db.js', () => { |
||
| 21 | before(() => rm(rungPath)); |
||
| 22 | |||
| 23 | describe('Database', () => { |
||
| 24 | it('should get undefined when reading from empty db', () => { |
||
| 25 | return read(extensionName) |
||
| 26 | .then(result => { |
||
| 27 | expect(result).to.equals(undefined); |
||
| 28 | }); |
||
| 29 | }); |
||
| 30 | |||
| 31 | it('should store an object in the database', () => { |
||
| 32 | const source = compileES6(` |
||
| 33 | export default { |
||
| 34 | extension(context) { |
||
| 35 | return { |
||
| 36 | alerts: {}, |
||
| 37 | db: { counter: 1 } |
||
| 38 | }; |
||
| 39 | } |
||
| 40 | }; |
||
| 41 | `); |
||
| 42 | |||
| 43 | return runAndGetAlerts({ name: extensionName, source }, {}) |
||
| 44 | .then(result => { |
||
| 45 | expect(result.db.counter).to.equals(1); |
||
| 46 | expect(dbPath).to.be.a.file(); |
||
| 47 | }); |
||
| 48 | }); |
||
| 49 | |||
| 50 | it('should break when passing invalid type to db', () => { |
||
| 51 | const source = compileES6(` |
||
| 52 | export default { |
||
| 53 | extension(context) { |
||
| 54 | return { |
||
| 55 | alerts: {}, |
||
| 56 | db: x => x |
||
| 57 | }; |
||
| 58 | } |
||
| 59 | }; |
||
| 60 | `); |
||
| 61 | |||
| 62 | return runAndGetAlerts({ name: extensionName, source }, {}) |
||
| 63 | .then(result => { |
||
| 64 | throw new Error('It should break'); |
||
| 65 | }) |
||
| 66 | .catch(err => { |
||
| 67 | expect(err.message).to.match(/Unsupported type Function/); |
||
| 68 | }); |
||
| 69 | }); |
||
| 70 | |||
| 71 | it('should read data from db', () => { |
||
| 72 | return read(extensionName) |
||
| 73 | .then(result => { |
||
| 74 | expect(result.counter).to.equals(1); |
||
| 75 | }); |
||
| 76 | }); |
||
| 77 | |||
| 78 | it('should update an object in the database', () => { |
||
| 79 | const source = compileES6(` |
||
| 80 | export default { |
||
| 81 | extension(context) { |
||
| 82 | return { |
||
| 83 | alerts: {}, |
||
| 84 | db: { counter: context.db.counter + 1 } |
||
| 85 | }; |
||
| 86 | } |
||
| 87 | }; |
||
| 88 | `); |
||
| 89 | |||
| 90 | return read(extensionName) |
||
| 91 | .then(db => runAndGetAlerts({ name: extensionName, source }, { db })) |
||
| 92 | .then(result => { |
||
| 93 | expect(result.db.counter).to.equals(2); |
||
| 94 | }); |
||
| 95 | }); |
||
| 96 | |||
| 97 | it('should preserve the file when passed there isn\'t db', () => { |
||
| 98 | const source = compileES6(` |
||
| 99 | export default { |
||
| 100 | extension(context) { |
||
| 101 | return { |
||
| 102 | alerts: {} |
||
| 103 | }; |
||
| 104 | } |
||
| 105 | }; |
||
| 106 | `); |
||
| 107 | |||
| 108 | return runAndGetAlerts({ name: extensionName, source }, {}) |
||
| 109 | .then(() => { |
||
| 110 | expect(dbPath).to.be.a.file() |
||
| 111 | .with.contents(JSON.stringify({ counter: 2 })); |
||
| 112 | }); |
||
| 113 | }); |
||
| 114 | |||
| 115 | it('should preserve the file when passed undefined', () => { |
||
| 116 | const source = compileES6(` |
||
| 117 | export default { |
||
| 118 | extension(context) { |
||
| 119 | return { |
||
| 120 | alerts: {}, db: undefined |
||
| 121 | }; |
||
| 122 | } |
||
| 123 | }; |
||
| 124 | `); |
||
| 125 | |||
| 126 | return runAndGetAlerts({ name: extensionName, source }, {}) |
||
| 127 | .then(() => { |
||
| 128 | expect(dbPath).to.be.a.file() |
||
| 129 | .with.contents(JSON.stringify({ counter: 2 })); |
||
| 130 | }); |
||
| 131 | }); |
||
| 132 | }); |
||
| 133 | |||
| 134 | describe('Command line database', () => { |
||
| 135 | it('should cause an error on invalid option', () => { |
||
| 136 | const stream = createStream(['db', 'write']); |
||
| 137 | |||
| 138 | return stream.once('data') |
||
| 139 | .then(result => { |
||
| 140 | expect(result).to.match(/Unknown option write/); |
||
| 141 | }) |
||
| 142 | .finally(stream.close); |
||
| 143 | }).timeout(5000); |
||
| 144 | |||
| 145 | it('should fail to read database when a file doesn\'t exist', () => { |
||
| 146 | const stream = createStream(['db', 'read']); |
||
| 147 | return stream.once('data') |
||
| 148 | .then(error => { |
||
| 149 | expect(error).to.match(/Unable to read database/); |
||
| 150 | }); |
||
| 151 | }).timeout(5000); |
||
| 152 | |||
| 153 | it('should read database via rung-cli', () => { |
||
| 154 | const source = compileES6(` |
||
| 155 | export default { |
||
| 156 | extension(context) { return { alerts: {}, db: { |
||
| 157 | dragQueen: 'sharon' |
||
| 158 | } }; } |
||
| 159 | }; |
||
| 160 | `); |
||
| 161 | |||
| 162 | return runAndGetAlerts({ name: 'rung-cli', source }, {}) |
||
| 163 | .then(() => { |
||
| 164 | const stream = createStream(['db', 'read']); |
||
| 165 | return stream.once('data') |
||
| 166 | .then(yaml => { |
||
| 167 | expect(yaml).to.equals('dragQueen: sharon\n'); |
||
| 168 | }); |
||
| 169 | }) |
||
| 170 | }).timeout(10000); |
||
| 171 | |||
| 172 | it('should drop database via rung db clear', () => { |
||
| 173 | const source = compileES6(` |
||
| 174 | export default { |
||
| 175 | extension(context) { return { alerts: {}, db: { |
||
| 176 | dragQueen: 'alaska' |
||
| 177 | } }; } |
||
| 178 | }; |
||
| 179 | `); |
||
| 180 | |||
| 181 | return runAndGetAlerts({ name: extensionName, source }, {}) |
||
| 182 | .then(() => { |
||
| 183 | const stream = createStream(['db', 'clear']); |
||
| 184 | return stream.after() |
||
| 185 | .then(() => { |
||
| 186 | expect(path.join(os.homedir(), '.rung', 'rung-cli.db')) |
||
| 187 | .to.not.be.a.path(); |
||
| 188 | }); |
||
| 189 | }) |
||
| 190 | }).timeout(20000); |
||
| 191 | }); |
||
| 192 | }); |
||
| 193 |